"""
@author：yiwang
@file_name: __init__.py
@create date: 2024-03-26
@file_description：
"""

import re
from flask_restful import reqparse,Resource
from flask import jsonify,request
from flask_login import login_required

from apps.API.project.db_project import *
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from apps.API.common.db_table_config import *
from apps.API.common.common_generate_id import id_generator

gen=id_generator(name=common_tables.receipt_db["name"])

'''
    Service API 
'''
class interfaceService(Resource):
    def get(self):
        try:
            ### SQL: query service in database ###
            cols = '''
                id,
                service_id,
                service_name
            '''
            query_service_sql = '''
                SELECT {cols} FROM {table_name}
            '''.format(
                table_name=common_tables.service_db["name"],
                cols=cols,
            )
            service_result=dic_query_info(query_service_sql)
            response=jsonify(service_result)
            response.status_code = 202
            return response
        except Exception as e:
            lg.error(e)
    '''
        Query project id 
    '''
    def post(self):
        try:
            select_proj_ids = request.json["project_id"]
            query_proj_list = ""
            for project_id in select_proj_ids:
                query_proj_list += "\"" + project_id + "\","
            query_proj_list=query_proj_list.rstrip(",")
            ### SQL: query selected projects ###
            cols = '''
                id,
                project_id,
                client_id,
                sample_id,
                sample_name,
                lot_number,
                size,
                sample_amount,
                arrive_time,
                instock_time,
                create_time,
                modify_time
            '''
            query_proj_sql='''
                SELECT {cols} FROM {table_name} WHERE {select_col} IN ({select_values})
            '''.format(
                    cols=cols,
                    table_name=common_tables.sample_db["name"],
                    select_col="project_id",
                    select_values=query_proj_list
            )
            base_infos=dic_query_info(query_proj_sql)
            base_infos["create_time"]=shift_format(base_infos["create_time"])
            base_infos["modify_time"]=shift_format(base_infos["modify_time"])
            response=jsonify(base_infos)
            response.status_code = 202
            return response
        except Exception as e:
            lg.error(e)
            return 400